package com.szh.mysobproject.ui.fragment

import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import by.kirich1409.viewbindingdelegate.viewBinding
import com.szh.mysobproject.R
import com.szh.mysobproject.action.OnBack2TopListener
import com.szh.mysobproject.action.StatusAction
import com.szh.mysobproject.app.AppActivity
import com.szh.mysobproject.app.AppFragment
import com.szh.mysobproject.app.TitleBarFragment
import com.szh.mysobproject.databinding.ArticleListFragmentBinding
import com.szh.mysobproject.ui.activity.BrowserActivity
import com.szh.mysobproject.ui.activity.ImagePreviewActivity
import com.szh.mysobproject.ui.adapter.AdapterDelegate
import com.szh.mysobproject.ui.adapter.ArticleAdapter
import com.szh.mysobproject.util.SUNNY_BEACH_ARTICLE_URL_PRE
import com.szh.mysobproject.util.SimpleLinearSpaceItemDecoration
import com.szh.mysobproject.util.dp
import com.szh.mysobproject.util.loadStateListener
import com.szh.mysobproject.viewmodel.ArticleViewModel
import com.szh.mysobproject.widget.StatusLayout
import kotlinx.coroutines.flow.collectLatest

class ArticleListFragment : TitleBarFragment<AppActivity>(), StatusAction, OnBack2TopListener {

    private val mBinding by viewBinding<ArticleListFragmentBinding>()
    private val mArticleViewModel by viewModels<ArticleViewModel>()
    private val mAdapterDelegate = AdapterDelegate()
    private val mArticleAdapter = ArticleAdapter(mAdapterDelegate)
    private val loadStateListener = loadStateListener(mArticleAdapter) {
        mBinding.refreshLayout.finishRefresh()
    }

    override fun getLayoutId(): Int = R.layout.article_list_fragment

    override fun initView() {
        mBinding.rvArticleList.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = mArticleAdapter
            addItemDecoration(SimpleLinearSpaceItemDecoration(4.dp))
        }
    }

    override fun initData() {
        viewLifecycleOwner.lifecycleScope.launchWhenCreated {
            mArticleViewModel.getArticleListByCategoryId("recommend").collectLatest {
                mArticleAdapter.submitData(it)
            }
        }
    }

    override fun initEvent() {
        mBinding.refreshLayout.setOnRefreshListener {
            mArticleAdapter.refresh()
        }
        mArticleAdapter.addLoadStateListener(loadStateListener)
        mAdapterDelegate.setOnItemClickListener { _, position ->
            val item = mArticleAdapter.snapshot()[position] ?: return@setOnItemClickListener
            val url = "$SUNNY_BEACH_ARTICLE_URL_PRE${item.id}"
            BrowserActivity.start(requireContext(), url)
        }
        mArticleAdapter.setOnMenuItemClickListener { view, item, position ->
            when (view.id) {
                R.id.ll_share -> {}
            }
        }
        mArticleAdapter.setOnNineGirdClickListener { source, index ->
            ImagePreviewActivity.start(requireContext(), source, index)
        }
    }

    override fun getStatusLayout(): StatusLayout = mBinding.slArticleLayout

    override fun onBack2Top() {
        mBinding.rvArticleList.smoothScrollToPosition(0)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mArticleAdapter.removeLoadStateListener(loadStateListener)
    }

    override fun isStatusBarEnabled(): Boolean {
        return !super.isStatusBarEnabled()
    }

    companion object {
        @JvmStatic
        fun newInstance() = ArticleListFragment()
    }

}